﻿/**
*Dialog class
*auth:lww
*version:1.0
*date:2014-10-11
*/
function Dialog(context) {
    this.id = "";
    this.name = "Dialog";
    this.version = "1.0.0";
    this.mContext = context;
    this.mLayoutParams = null;
    this.mRootViewObject = null;
    this.mViewObject = null;
    this.isCenter = false;
    this.isDrag = false;
    this.initFlag = false;
}

Dialog.prototype.isActivity = function () {
    return this.initFlag;
};

Dialog.prototype.invalidate = function () {
    if (this.mRootViewObject == null) {
        this.mRootViewObject = new FrameLayout(this.mContext, this.mContext.getLayerIndex());
    }
    else {
        this.mRootViewObject.setIndex(this.mContext.getLayerIndex());
    }
    if (this.mLayoutParams != null) {
        this.mRootViewObject.setLayoutParams(this.mLayoutParams);
    }
    return this.mRootViewObject.view();
};

Dialog.prototype.view = function () {
    return this.invalidate();
};

Dialog.prototype.getId = function () {
    if (this.id == null || this.id == "") {
        this.id = id();
    }
    return this.id;
};

Dialog.prototype.setId = function (ids) {
    this.id = ids;
};

Dialog.prototype.onWinResize = function () {
    if (this.mLayoutParams != null && this.isCenter) {
        var x = (getWinWidth() - this.mLayoutParams.mWidth) / 2;
        var y = (getWinHeight() - this.mLayoutParams.mHeight) / 2;
        if (this.mLayoutParams.mWidth <= 0) {
            x = (getWinWidth()) / 3;
        }
        if (this.mLayoutParams.mHeight <= 0) {
            y = (getWinHeight()) / 3;
        }
        this.mLayoutParams.mLeftMargin = x;
        this.mLayoutParams.mTopMargin = y;
        this.invalidate();    
        if (this.isDrag) {
            this.setDrag(this.isDrag);
        }
    }
};

Dialog.prototype.setLayoutParams = function (pars, isCenter) {
    if (pars != null) {
        this.mLayoutParams = pars;
        if (this.mLayoutParams.mWidth <= 0) {
            this.mLayoutParams.mWidth = (getWinWidth()) / 2;
        }
        this.isCenter = isCenter;
        if (isCenter) {
            this.mLayoutParams.mLeftMargin = (getWinWidth() - pars.mWidth) / 2;
            this.mLayoutParams.mTopMargin = (getWinHeight() - pars.mHeight) / 2;
            var o = this; window.onresize = function () {
                o.onWinResize();
            };
        }
        this.invalidate();
    }
};

Dialog.prototype.getLayoutParams = function () {
    return this.mLayoutParams;
};

Dialog.prototype.setBackground = function (drawable) {
    if (this.mRootViewObject != null
        && drawable != null
        && typeof (drawable) === "object") {
        this.mRootViewObject.setBackground(drawable);
    }
};

Dialog.prototype.setContentView = function (viewObject) {
    if (this.mRootViewObject == null) {
        this.invalidate();
    }
    if(viewObject == null || typeof (viewObject) != 'object'){
        return;
    }   
    this.mViewObject = viewObject;
    this.mRootViewObject.removeAllView();
    this.mRootViewObject.addChildView(this.mViewObject.view());
    this.initFlag = true;
    if (typeof (viewObject.getWidth) != 'undefined'
        && viewObject.getWidth() != null
        && this.mLayoutParams != null) {
        this.mLayoutParams.mWidth = viewObject.getWidth();
        this.onWinResize();
    }
    if (typeof (viewObject.getHeight) != 'undefined'
        && viewObject.getHeight() != null
        && this.mLayoutParams != null) {
        this.mLayoutParams.mHeight = viewObject.getHeight();
        this.onWinResize();
    }
    var o = this;
    viewObject.onClose = function(refObj, type){
        o.close(type);
    };   
};

Dialog.prototype.setDrag = function (isDrag) {
    this.isDrag = isDrag;
    {
        if (this.mViewObject != null && typeof (this.mViewObject.getDragView) != 'undefined') {
            var dragView = this.mViewObject.getDragView();
            if (dragView != null && typeof (dragView) != 'undefined') {
                var maxX = getWinWidth();
                var maxY = getWinHeight();
                if (this.mLayoutParams != null) {
                    maxX -= this.mLayoutParams.mWidth;
                    maxY -= this.mLayoutParams.mHeight;
                }
                if (window.dragInstance != null && window.dragInstance != 'undefined') {
                    if (this.isDrag)
                        window.dragInstance.init(dragView, this.view(), 0, maxX, 0, maxY);
                    else {
                        window.dragInstance.cancel(dragView);
                    }
                }
            }
        }
    }
};

Dialog.prototype.onClose = function (arg, obj) {
    //set to your callback:close interface   
};

Dialog.prototype.close = function (arg) {
    if (this.mRootViewObject != null && this.initFlag) {
        //call close interface
        try {
            this.onClose(arg, this);
        } catch (e) {
            //add your code here
        }
        //close dialog
        var pNode = this.mRootViewObject.view().parentNode;
        if (pNode != null) {
            pNode.removeChild(this.mRootViewObject.view());
            if (this.isCenter) {
                window.onresize = null;
            }
        }
        //reset
        this.reset();
    }
};

Dialog.prototype.reset = function () {
    if (this.mRootViewObject != null) {
        this.mRootViewObject.removeAllView();
        this.mViewObject = null;
        this.initFlag = false;
    }
};

markscript("Dialog");
//end Dialog.js